var row =
	{ 
		init : function()
			{
				this.tipo = new Ext.form.ComboBox
		            ({
		                fieldLabel: 'Tipo',
		                name: 'data['+_PHP_.model+'][tipo_id]',
		                allowBlank: false,
		                width: 350,
		                height: 100,
		                
		                hiddenName: 'data['+_PHP_.model+'][tipo_id]',
		                editable: false,
		                valueField: 'id',
		                displayField: 'nome',
		                mode: 'local',
		                triggerAction: 'all',
		                lastQuery: '',
		                store: new Ext.data.SimpleStore
							({
				           		fields: ['id', 'nome'],
				           		data: _PHP_.form.tipos
				           	}),
						listeners:
							{
								'select': function(combo, record, index)
									{
										row.newsletter.setDisabled(record.get('id') != 7);
									}
							}
		            });
					
				this.nome = new Ext.form.TextField
		            ({
		                fieldLabel: 'Nome',
		                name: 'data['+_PHP_.model+'][nome]',
		                allowBlank: false,
		                width: 350
		            });
					
				this.cliente = new Ext.form.ComboBox
		            ({
						fieldLabel: 'Cliente',
		                name: 'data[Newsletter][cliente_id]',
		                width: 350,
		                height: 100,
						allowBlank: false,
						disabled: true,
		                
		                hiddenName: 'data[Newsletter][cliente_id]',
		                editable: false,
		                valueField: 'ClientID',
		                displayField: 'Name',
		                mode: 'local',
		                triggerAction: 'all',
		                lastQuery: '',
		                store: new Ext.data.JsonStore
							({
							    url: _PHP_.url.newsletter.cliente,
								id: 'ClientID',
					            root: 'rows',
							    fields:
									[
							            'ClientID',
							            'Name'
							        ]
							}),
						listeners:
							{
								'render': function(combo)
									{
										combo.store.on('load', function()
											{
												combo.setDisabled(false);
												combo.reset();
											});
									},
								'select': function(combo, record, index)
									{
										selected = record.get('ClientID');
										if(selected != combo.lastSelected) 
										{
											var lista = row.newsletter.items.get(1);
											
											lista.setDisabled(true);
											lista.setRawValue('Carregando ...');
											
											lista.store.load(
												{
													params: 
														{
															id: record.get('ClientID')
														}
												})
										}
										combo.lastValue = record.get('ClientID');
									}
							}
		            });
					
				this.lista = new Ext.form.ComboBox
		            ({
						fieldLabel: 'Listas',
		                name: 'data[Newsletter][lista_id]',
		                width: 350,
		                height: 100,
						allowBlank: false,
						disabled: true,
		                
		                hiddenName: 'data[Newsletter][lista_id]',
						editable: false,
		                valueField: 'ListID',
		                displayField: 'Name',
		                mode: 'local',
		                triggerAction: 'all',
		                lastQuery: '',
						//enableKeyEvents: true,

		                store: new Ext.data.JsonStore
							({
							    url: _PHP_.url.newsletter.lista,
					            root: 'rows',
							    fields:
									[
							            'ListID',
							            'Name'
							        ]
							}),
						listeners:
							{
								'render': function(combo)
									{
										combo.store.on('load', function()
											{
												combo.setDisabled(false);
												combo.reset();
											});
										//combo.el.on('keyup', combo.ctrlButton);
										//combo.el.on('mouseout', combo.ctrlButton);
									},
								'select': function(combo, record, index)
									{
										row.importar.setDisabled((typeof record.get('ListID') == 'undefined'))
									}
							}/*,
						ctrlButton: function(field, event)
							{
								var combo = row.lista;
								var lista = combo.getRawValue();
								var idx = combo.store.find('Name', lista)

								if(idx > -1)
								{
									combo.select(idx);
									row.importar.setDisabled(false);
								}
								else
								{
									combo.allowBlank = true;
									combo.setValue('');
									combo.setRawValue(lista);
									combo.allowBlank = false;
									
									row.importar.setDisabled(true);
								}
							}*/
		            });
				
				this.importar = new Ext.Button
					({
						text: 'Importar',
						icon: _PHP_.url.incroot+'img/admin/ext/application_view_list.png',
						cls: 'x-btn-text-icon',
						disabled: true,
						handler: function()
							{
								var id = row.lista.getValue();
								
								if(typeof row.importar.gridWin == 'undefined')
								{
									row.importar.gridWin = gridX.formWin.cloneConfig
										({
											layout: null,
											title: 'Lista: '+ row.cliente.getRawValue() + ' &rarr; ' + row.lista.getRawValue(),
											items: gridX.cloneConfig
												({
													titles: null,
													title: null,
													el : null,
													tbar: null,
													bbar: null,
													plugins: null,
													doEdit: null,
													doDelete: null,
													formWin: null,
													form: null,
													listeners: 
														{
															'render': function(grid)
																{
																	var size = grid.container.getSize();
																	grid.setSize(size.width - 40, size.height - 20);
																	grid.getStore().on('load', function()
																		{
																			grid.getSelectionModel().selectAll()
																		});
																}
														},
													cm: new Ext.grid.ColumnModel
												    	([
															{
																header: 'Nome',
													         	dataIndex: 'Name',
													         	width: 150
															},
															{
																header: 'Email',
													         	dataIndex: 'EmailAddress',
													         	width: 150
															},
															{
																header: 'Status',
													         	dataIndex: 'State',
													         	width: 40,
																align: 'center'
															},
															{
																header: 'Data',
													         	dataIndex: 'Date',
													         	width: 50,
																align: 'center',
																renderer: function(value)
																	{
																        return value ? value.dateFormat('H:i - d/m/y') : '';
																    }
															}
												        ]),
													store: new Ext.data.JsonStore
														({
														    url: _PHP_.url.newsletter.membro,
															id: 'id',
												            root: 'rows',
														    fields:
																[
																	'Name',
														            'EmailAddress',
																	 {
																	 	name: 'Date',
																		type: 'date',
																		dateFormat: 'Y-m-d h:i:s'
																	},
																	'State'
														        ]
														})
												}),
											buttons:
												[
													new Ext.SplitButton
														({
															text: 'Importar',
															menu: new Ext.menu.Menu
																({
																	items: 
																		[
																			{
																				text: 'Importar',
																				handler: function(button, event)
																					{
																						button.parent.setText('Importar');
																						row.importar.redundancia = true;
																					}
																			},
																			{
																				text: 'Importar evitando redundâncias',
																				handler: function(button, event)
																					{
																						button.parent.setText('Importar (Sem redundâncias)');
																						row.importar.redundancia = false;
																					}
																			}
																		]
																}),
															handler: function(button, event)
																{
																	var grid = row.importar.gridWin.items.get(0);
																	
																	if(grid.getSelectionModel().getCount() < 1) 
																	{
																		Ext.MessageBox.alert('Atenção', 'Por favor selecione os resgistros que deseja importar');
																	}
																	else 
																	{
																		var msgbox = Ext.MessageBox.wait('Processando ...', 'Aguarde');
																		
																		var contatos = [];
																		grid.getStore().each(function(store)
																			{
																				contatos.push
																					({
																						nome: store.data['Name'],
																						email: store.data['EmailAddress']
																					});
																			});
																																				
																		Ext.Ajax.request
																			({
																				url: _PHP_.url.newsletter.importar,
																				method: 'post',
																				params: 
																					{
																						redundancia: row.importar.redundancia,
																						contatos: Ext.util.JSON.encode(contatos)
																					},
																				callback: function(options, success, response)
																					{
																						msgbox.hide();
																						
																						var response = Ext.util.JSON.decode(response.responseText); 
																						if(response.success === 'true')
																						{
																							msgbox.alert('Processado', 'Contatos importantos com sucesso');
																						}
																						else
																						{
																							msgbox.alert('Erro', response.erro);
																						}
																					}
																			});
																	}
																},
															listeners:
																{
																	'render': function(button)
																		{
																			button.menu.items.each(function(item)
																				{
																					item.parent = button;
																				});
																			button.menu.items.get(0).handler(button.menu.items.get(0));
																		}
																}
														}),
													gridX.formWin.buttons[0].cloneConfig
														({
															text: 'Cancelar',
															handler: function()
																{
																	row.importar.gridWin.hide();
																}
														})
												],
											listeners:
												{
													'hide': function(obj)
														{
															var store = obj.items.get(0).getStore();
															store._olddata = store.data.clone();
															store.removeAll();
														}
												}
										});
								}
								
								var grid = row.importar.gridWin.items.get(0);
								var store = grid.getStore();
								
								row.importar.gridWin.show();
								grid.getColumnModel().defaultSortable = true;

								if(!store.lastOptions || store.lastOptions.params.id != id)
								{
									store.load
										({
											params:
												{
													id: id
												}
										});
								}
								else
								{
									store.data = store._olddata;
									grid.getView().refresh();
									store.fireEvent('load');
								}
							}
					});
					
				this.informacoes = new Ext.form.FieldSet
					({
						title: 'Informações',
						height: 'auto',
						items: 
							[
								this.tipo,
								this.nome
							]
					});
				
				this.newsletter = new Ext.form.FieldSet
					({
						title: 'Newsletter',
						disabled: true,
						height: 'auto',
						buttonAlign: 'center',
						items: 
							[
								this.cliente,
								this.lista
							],
						listeners:
							{
								'enable': function(fieldset)
									{
										var cliente = fieldset.items.get(0);
										var lista = fieldset.items.get(1);

										if(!cliente.store.lastOptions)
										{
											cliente.setRawValue('Carregando ...');
											cliente.store.load();
										}
										else 
										{
											cliente.setDisabled(false);
											lista.setDisabled(true);
										}
									}
							},
						buttons:
							[
								this.importar
							]
					});
		        
				gridX.form.add(this.informacoes, this.newsletter);
				gridX.form.items.each(function()
					{
						this.items.each(function()
							{
								util.applyDefaults(this, gridX.form.defaults);
							});
					});
				gridX.formWin.on('hide', function()
					{
						row.newsletter.setDisabled(true);
						row.cliente.setDisabled(true);
						row.lista.setDisabled(true);
						row.importar.setDisabled(true);
					});
				gridX.formWin.height = 310;
				gridX.formWin.width = 500;
			}
	};